//
//  FDMFindBigCollectionViewCell.m
//  Entertainment
//
//  Created by dllo on 16/4/19.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMFindBigCollectionViewCell.h"
#import "FDMPushWheelImages.h"
#import "FDMPushSectionOneCollectionViewCell.h"
#import "FDMPushFindNewsModel.h"
#import "FDMPushGuessYourFavoriteCollectionView.h"
#import "FDMPushSomeOnePushCollectionViewCell.h"
#import "FDMSomeOnePushModel.h"
#import "FDMHotRecommendsModel.h"
#import "FDMHotRecommendsHeaderView.h"
#import "FDMPushWheelModel.h"

@interface FDMFindBigCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) FDMBaseCollectionView *findCollectionView;/**< 大的集合视图第0个cell */

@property (nonatomic, retain) NSMutableArray *findNewsModelArr;/**< 发现新奇的model数组 */

@property (nonatomic, retain) NSDictionary *editorRecommendDic;/**< 小编推荐的字典 */

@property (nonatomic, retain) NSMutableArray *editorRecommendAlbumsModelArr;/**< 小编推荐model数组 */

@property (nonatomic, retain) NSMutableArray *hotRecommendsModelArr;/**< 热门推荐model数组 */

@property (nonatomic, retain) NSMutableArray *focusImagesModelArr;/**< 轮播图model数组 */

@end
@implementation FDMFindBigCollectionViewCell

- (void)dealloc {
    [self.findCollectionView release];
    [self.findNewsModelArr release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 创建集合视图
        [self createCollectionView];
        // 数据处理
        [self dataHandel];
        // 初始化数组
        self.findNewsModelArr = [NSMutableArray array];
        self.editorRecommendAlbumsModelArr = [NSMutableArray array];
        self.hotRecommendsModelArr = [NSMutableArray array];
        self.editorRecommendDic = [NSDictionary dictionary];
        self.focusImagesModelArr = [NSMutableArray array];
    }
    return self;
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.findCollectionView = [[FDMBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 157) collectionViewLayout:layout];
    self.findCollectionView.delegate = self;
    self.findCollectionView.dataSource = self;
    [self.contentView addSubview:self.findCollectionView];
    self.findCollectionView.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];
    // 注册重用池
    // 轮播图
    [self.findCollectionView registerClass:[FDMPushWheelImages class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMBaseWheelImage"];
    // 分区0
    [self.findCollectionView registerClass:[FDMPushSectionOneCollectionViewCell class] forCellWithReuseIdentifier:@"FDMPushSectionOneCollectionViewCell"];
    // 猜你喜欢
    [self.findCollectionView registerClass:[FDMPushGuessYourFavoriteCollectionView class] forCellWithReuseIdentifier:@"FDMPushGuessYourFavoriteCollectionView"];
    // 小编推荐
    [self.findCollectionView registerClass:[FDMPushSomeOnePushCollectionViewCell class] forCellWithReuseIdentifier:@"FDMPushSomeOnePushCollectionViewCell"];
    // 小编推荐头视图
    [self.findCollectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMHotRecommendsHeaderViewSectionOne"];
    // 热门推荐的头视图
    [self.findCollectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMHotRecommendsHeaderView"];
    // 热门推荐
    [self.findCollectionView registerClass:[FDMPushSomeOnePushCollectionViewCell class] forCellWithReuseIdentifier:@"DetailHotPushCollectionViewCell"];
}

#pragma mark - collectionViewDelegate
// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2 + self.hotRecommendsModelArr.count;
}

// cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 100);
    }
    if (indexPath.section > 0 && indexPath.section < 2 + self.hotRecommendsModelArr.count) {
        return CGSizeMake((SCREEN_WIDTH - 50) / 3, 140);
    } else {
        return CGSizeZero;
    }
}

// 头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 150);
    }
    
    return CGSizeMake(SCREEN_WIDTH, 30);
}

// cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return self.editorRecommendAlbumsModelArr.count;
    }
    if (section > 1 && section < 2 + self.hotRecommendsModelArr.count) {
        FDMHotRecommendsModel *model = self.hotRecommendsModelArr[section - 2];
        return model.detailModelArr.count;
    } else {
        return 0;
    }
    
    
   
}

// 头视图赋值
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMPushWheelImages *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMBaseWheelImage" forIndexPath:indexPath];
            if (self.focusImagesModelArr.count) {
                header.picArr = self.focusImagesModelArr;
            }
            header.backgroundColor = [UIColor redColor];
            return header;
        }
    }
    if (indexPath.section == 1) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMHotRecommendsHeaderViewSectionOne" forIndexPath:indexPath];
            header.titleLabel.text = self.editorRecommendDic[@"title"];
            return header;
        }
    }
    if (indexPath.section > 1 && indexPath.section < 2 + self.hotRecommendsModelArr.count) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMHotRecommendsHeaderView" forIndexPath:indexPath];
            FDMHotRecommendsModel *model = self.hotRecommendsModelArr[indexPath.section - 2];
            header.model = model;
            return header;
        }
    } else {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMPushWheelImages *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMBaseWheelImage" forIndexPath:indexPath];
            header.backgroundColor = [UIColor redColor];
            return header;
        }
    }
    return nil;
}

// cell 赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        FDMPushSectionOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMPushSectionOneCollectionViewCell" forIndexPath:indexPath];
        cell.dataSourceModelArr = self.findNewsModelArr;
        return cell;
    }
    if (indexPath.section == 1) {
        FDMPushSomeOnePushCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMPushSomeOnePushCollectionViewCell" forIndexPath:indexPath];
        FDMSomeOnePushModel *model = self.editorRecommendAlbumsModelArr[indexPath.row];
        cell.modle = model;
        return cell;
    }
    if (indexPath.section > 1 && indexPath.section < self.hotRecommendsModelArr.count + 2) {
        FDMHotRecommendsModel *model = self.hotRecommendsModelArr[indexPath.section - 2];
        FDMPushSomeOnePushCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailHotPushCollectionViewCell" forIndexPath:indexPath];
        FDMSomeOnePushModel *detailModel = model.detailModelArr[indexPath.row];
        cell.detailModel = detailModel;
        return cell;
    } else {
        FDMPushGuessYourFavoriteCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMPushGuessYourFavoriteCollectionView" forIndexPath:indexPath];
        return cell;
    }
}

- (void)dataHandel {
    
    // 大的滚动视图数据
    NSString *url = @"http://mobile.ximalaya.com/mobile/discovery/v2/recommends?channel=and-f6&device=android&includeActivity=true&includeSpecial=true&scale=2&statEvent=pageview%2Ftab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&version=4.3.80";
    
    [APPTools GETWithURL:url par:nil success:^(id responseObject) {
        NSArray *arr = responseObject[@"discoveryColumns"][@"list"];
        /** 发现新奇model数组 */
        self.findNewsModelArr = [FDMPushFindNewsModel baseModelHandelWithArray:arr];
        
        /** 小编推荐字典 */
        self.editorRecommendDic = responseObject[@"editorRecommendAlbums"];
        
        /** 小编推荐model数组 */
        self.editorRecommendAlbumsModelArr = [FDMSomeOnePushModel baseModelHandelWithArray:self.editorRecommendDic[@"list"]];
        
        /** 热门推荐model数组 */
        self.hotRecommendsModelArr = [FDMHotRecommendsModel baseModelHandelWithArray:responseObject[@"hotRecommends"][@"list"]];
        
        /** 轮播图数组 */
        self.focusImagesModelArr = responseObject[@"focusImages"][@"list"];
        [self.findCollectionView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    NSString *guessYourFavoriteStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/guessYouLike/unlogin?pageId=1&pageSize=6&spanId=7053998904469602948&traceId=2113799776555832494&viewId=2131558404";
    
    
    [APPTools GETWithURL:guessYourFavoriteStr par:nil success:^(id responseObject) {
//        NSLog(@"%@", responseObject);
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
